﻿Imports Application=System.Windows.Application
Imports MessageBox=System.Windows.MessageBox
Imports System.Windows.Forms

Namespace TweeVo
	''' <summary>
	''' Interaction logic for App.xaml
	''' </summary>
	Partial Public Class App
		Inherits Application
		Private _window As TweeVoWindow
		Private _notifyIcon As NotifyIcon

		Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
			' create a tray icon and setup an event handler for double-clicking it
			_notifyIcon = New NotifyIcon()
			_notifyIcon.Icon = My.Resources.Icon
			_notifyIcon.Visible = True
			AddHandler _notifyIcon.DoubleClick, AddressOf notifyIcon_DoubleClick

			' setup 2 menu items
			Dim items() As MenuItem = { New MenuItem("&Settings", AddressOf Settings_Click) With {.DefaultItem = True}, New MenuItem("-"), New MenuItem("&Exit", AddressOf Exit_Click) }
			_notifyIcon.ContextMenu = New ContextMenu(items)

			' create the window and show it if we're not configured
			_window = New TweeVoWindow()

			If TweeVoSettings.Default.TiVos Is Nothing OrElse TweeVoSettings.Default.TiVos.Count = 0 Then
				_window.Show()
			Else
				TiVoPoller.Start()
			End If
		End Sub

		Private Sub Settings_Click(ByVal sender As Object, ByVal e As EventArgs)
			_window.Show()
		End Sub

		Private Sub Exit_Click(ByVal sender As Object, ByVal e As EventArgs)
			Application.Current.Shutdown()
		End Sub

		Private Sub App_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
			' remove the icon from the tray on exit
			_notifyIcon.Dispose()
		End Sub

		Private Sub App_DispatcherUnhandledException(ByVal sender As Object, ByVal e As System.Windows.Threading.DispatcherUnhandledExceptionEventArgs)
			' display any unhandled exception
			MessageBox.Show(e.Exception.ToString())
		End Sub

		Private Sub notifyIcon_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
			' show the window in the normal state
			_window.Show()
			_window.WindowState = WindowState.Normal
		End Sub

		Public Shared Sub OnRequestNavigation(ByVal sender As Object, ByVal e As RoutedEventArgs)
			' all uperlinks will be passed through this handler and will start them in the default web browser
			Dim hl As Hyperlink = TryCast(e.OriginalSource, Hyperlink)
			If hl IsNot Nothing Then
				Process.Start(hl.NavigateUri.ToString())
			End If
		End Sub

		Public Sub ShowBalloonTip(ByVal msg As String)
			_notifyIcon.ShowBalloonTip(10000, "TweeVo", msg, ToolTipIcon.Info)
		End Sub
	End Class
End Namespace
